package com.hongying.producer;

import com.hongying.constant.RabbitMqConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class TopicSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(String exchange,String routingKey,String message){
        log.info("Exchange:{}-routingKey:{}-Message:{}",exchange,routingKey,message);

        this.rabbitTemplate.convertAndSend(exchange,routingKey,message);
    }

    public void delaySend(String exchange,String routingKey,String message){
        rabbitTemplate.convertAndSend(
                exchange,
                routingKey,
                "发送消息：" + message,
                new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        //设置过期时间
                        message.getMessageProperties().setDelay(Integer.valueOf(30));
                        return message;
                    }
                },
                new CorrelationData());
    }


}
